package testing;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;

import com.thesis.algorithm.SequenceMatching;
import com.thesis.algorithm.FileConverter;
import com.thesis.algorithm.HMM;
import com.thesis.algorithm.Sequence;
import com.thesis.algorithm.SequenceElement;
import com.thesis.downloader.BusInfoDownloader;
import com.thesis.downloader.BuytTPHCMinfo;
import com.thesis.downloader.busFullRoute;
//import com.thesis.mDataStructure.FileWrapper;
import com.thesis.utils.*;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.Collections;
import com.thesis.VeloEstiCellID.VeloEstiCellID;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            long startTime = System.currentTimeMillis();
//                        BusControl busController = new BusControl();
//                        FileConverter fc = new FileConverter(CONSTANTS.TEST_DATA_FOLDER);
//                        LinkedList<Sequence> _TESTDATA = busController.produceTrainSequence(fc.listOfDevices);
//                        for (Sequence seq: _TESTDATA)
//                        {
//                            System.out.println(seq.refineSequence().toString());
//                        }
//                        for (short key1 : busController.distanceBusStops.keySet()) {
//                            System.out.println ("BUS ID: " + key1);
//                            Hashtable<Character, Hashtable <Character, Double>> tmp1 = busController.distanceBusStops.get(key1);
//                            for (char key2 : tmp1.keySet()) {
//                                Hashtable <Character, Double> tmp2 = tmp1.get(key2);
//                                for (char key3 : tmp2.keySet()) {
//                                    System.out.println("FROM " + (int) key2 + " TO " + (int) key3 + ": " + tmp2.get(key3));
//                                }
//                            }
////                        }
//                        Sequence seq = StringToSequence("==s8882:o53159:l20464:r47==s8882:o40532:l20464:r14==s8882:o53159:l20464:r7==s8883:o53159:l20464:r3==s8883:o40532:l20464:r19==s8883:o40531:l20464:r3==s8884:o40531:l20464:r38==s8884:o40533:l20464:r12==s8885:o40533:l20464:r25==s8885:o7321:l20464:r18==s8885:o40533:l20464:r12==s8885:o7321:l20464:r6==s8886:o7321:l20464:r34==s8886:o53178:l20464:r20==s8886:o7321:l20464:r33==s8886:o53178:l20464:r96==s8886:o7321:l20464:r31==s8886:o53178:l20464:r115==s8886:o7321:l20464:r14==s8887:o53178:l20464:r38==s8888:o53178:l20464:r4==s8888:o53179:l20464:r11");
//                        BusControl bc = new BusControl();
//                        FileConverter fc = new FileConverter(CONSTANTS.TEST_DATA_FOLDER);
//                        LinkedList<Sequence> _TESTDATA = bc.produceTrainSequence(fc.listOfDevices);
//                        VelocityEstimateSequence ves = new VelocityEstimateSequence(bc);
//                        for (short key1 : bc.distanceBusStops.keySet()) {
//                            if (key1 != (short) -8)
//                                continue;
//                            System.out.println ("BUS ID: " + key1);
//                            Hashtable<Character, Hashtable <Character, Double>> tmp1 = bc.distanceBusStops.get(key1);
//                            for (char key2 : tmp1.keySet()) {
//                                Hashtable <Character, Double> tmp2 = tmp1.get(key2);
//                                for (char key3 : tmp2.keySet()) {
//                                    System.out.println("FROM " + (int) key2 + " TO " + (int) key3 + ": " + tmp2.get(key3));
//                                }
//                            }
//                        }
//                        ves.Add(seq, (short)74);
//                        System.out.println(ves.returnVelocity(2)*3600 + " " +ves.lastTimeStamp);
//                        ves.Print();

            // String test =
            // "s10410:o52318:l20365:r26==s10411:o52318:l20365:r7==s10411:o43152:l20365:r45==s10412:o43152:l20365:r12==s10412:o43158:l20365:r15==s10413:o43158:l20365:r11==s10413:o43152:l20365:r9==s10413:o55869:l20365:r12==s10413:o40260:l20823:r21==s10413:o218:l20823:r13==s10413:o40250:l20823:r114==s10414:o40250:l20823:r46==s10415:o40250:l20823:r8==s10415:o45963:l20452:r44==s10200:o45963:l20452:r11==s10200:o45962:l20452:r31==s10200:o53777:l20452:r15==s10200:o49509:l20452:r28==s10200:o44841:l20455:r15==s10200:o52229:l20452:r13==s10200:o44841:l20455:r26==s10200:o14129:l20452:r9==s10200:o40678:l20452:r46==s10200:o43219:l20452:r8==s10200:o13429:l20452:r11==s10200:o13427:l20452:r4";
            // String [] stringsplit = test.split("s|:|o|r|=");
            // int control = 0;
            // Character c = '0';
            // int repeat = 0;
            // int timeStamp = 0;
            // BusControl bus = new BusControl();
            // VelocityEstimate ve = new VelocityEstimate(bus);
            // LinkedList<Character> m = new LinkedList<Character>();
            // for (String s : stringsplit)
            // {
            // if (s.compareTo("") == 0)
            // continue;
            // if (control == 0){
            // c = (char)Integer.parseInt(s);
            // control++;
            // } else if (control == 2 || control == 1){
            // control++;
            // } else if (control == 3) {
            // repeat = Integer.parseInt(s);
            // control = 0;
            // m= new LinkedList<Character>();
            // m.add(c);
            // ve.Add(timeStamp, m, new short[]{19});
            // timeStamp+=repeat;
            // }
            //
            // //System.out.println("---"+s+"---");
            // }
            // ve.Add(timeStamp, m, new short[]{19});
            // System.out.println(ve.returnVelocity(480, 600)*3600);
//			busID0: ==s8499:o48111:l20382:r3==s8500:o48111:l20382:r33==s8500:o18647:l20382:r17==s8501:o48102:l20382:r15==s8501:o48101:l20382:r5==s8502:o48101:l20382:r26==s7300:o48101:l20382:r16==s7300:o8415:l20382:r9==s7300:o45213:l20382:r1==s7300:o44401:l20382:r23==s7301:o44401:l20382:r53==s7302:o44401:l20382:r12==s7302:o48946:l20382:r7
//
//					busID0: ==s8499:o48111:l20382:r36==s8500:o18647:l20382:r17==s8501:o48102:l20382:r25==s8501:o48101:l20382:r65==s7300:o8415:l20382:r9==s7300:o45213:l20382:r12==s7300:o44401:l20382:r102==s7302:o48946:l20382:r34
//			Sequence result = StringToSequence("s8499:o48111:l20382:r3==s8500:o48111:l20382:r33==s8500:o18647:l20382:r17==s8501:o48102:l20382:r15==s8501:o48101:l20382:r5==s8502:o48101:l20382:r26==s7300:o48101:l20382:r16==s7300:o8415:l20382:r9==s7300:o45213:l20382:r1==s7300:o44401:l20382:r23==s7301:o44401:l20382:r53==s7302:o44401:l20382:r12==s7302:o48946:l20382:r7");
//			Sequence refinematchedsequence = StringToSequence("s8499:o48111:l20382:r36==s8500:o18647:l20382:r17==s8501:o48102:l20382:r25==s8501:o48101:l20382:r65==s7300:o8415:l20382:r9==s7300:o45213:l20382:r12==s7300:o44401:l20382:r102==s7302:o48946:l20382:r34");
//			
//			int i = 0;
//			int j = 0;
//			int countmatched = 0;
//			int countresult = 0;
//			SequenceElement seresult = null;
//			SequenceElement sematched = null;
//			boolean dontaddi = false;
//			while (true) {
//				seresult = result.get(j);
//				sematched = refinematchedsequence.get(i);
//				if (dontaddi == false)
//					countmatched += sematched.Repetition;
//				else
//					dontaddi = false;
//				countresult += seresult.Repetition;
//				
//				if (seresult.Observation == sematched.Observation)
//				{
//					if (countmatched == countresult)
//					{
//						i++;
//						j++;
//						countmatched = 0;
//						countresult = 0;
//					}
//					else if (countresult < countmatched)
//					{
//						j++;
//						dontaddi = true;
//					}
//					else
//					{
//						System.err.println("SUM TING RON");
//					}
//				}
//				else {
//					i++;
//					result.get(j-1).Repetition += (countmatched - countresult - seresult.Repetition);
//					countmatched = 0;
//					countresult = 0;
//				}
//				if (i == refinematchedsequence.size() && j == result.size())
//					break;
//				if (i == refinematchedsequence.size() || j == result.size())
//				{
//					if (refinematchedsequence.score() > result.score())
//						result.sequenceinFile.getLast().Repetition += (refinematchedsequence.score() - result.score());
//					break;
//				}
//					
//			}

//			ve.Add(t, (short) 19);
//			ve.Print();
//			System.out.println(ve.returnVelocity(1) * 3600);
//			System.out.println(ve.returnVelocity(2) * 3600);
//			System.out.println(ve.returnVelocity(3) * 3600);
//			System.out.println(ve.returnVelocity(4) * 3600);
//			System.out.println(ve.returnVelocity(5) * 3600);
//			System.out.println(ve.returnVelocity(6) * 3600);
            // m.add((char)(10435));
            // ve.Add(0, m, CONSTANTS.ALLBUSROUTESHORT);
            // ve.Add(42, m, CONSTANTS.ALLBUSROUTESHORT);
            // ve.Add(52, m, CONSTANTS.ALLBUSROUTESHORT);
            // ve.Add(75, m, CONSTANTS.ALLBUSROUTESHORT);
            // ve.Add(88, m, CONSTANTS.ALLBUSROUTESHORT);
            // ve.Add(107, m, CONSTANTS.ALLBUSROUTESHORT);
            // ve.Add(130, m, CONSTANTS.ALLBUSROUTESHORT);
            // ve.Add(223, m, CONSTANTS.ALLBUSROUTESHORT);
            // ve.Add(246, m, CONSTANTS.ALLBUSROUTESHORT);
            // ve.Add(274, m, CONSTANTS.ALLBUSROUTESHORT);
            // m = new LinkedList <Character> ();
            // m.add((char)(10436));
            // ve.Add(278, m, CONSTANTS.ALLBUSROUTESHORT);
            // ve.Add(300, m, CONSTANTS.ALLBUSROUTESHORT);

            // System.out.println(ve.returnVelocity(240, 300)*3600);

            // ==s8400:o36193:l20120:r26
            // ==s8400:o36302:l20120:r48
            // ==s8348:o36302:l20120:r22
            // ==s8348:o36301:l20120:r94
            // ==s8348:o36032:l20120:r41
            // ==s8348:o36033:l20120:r25
            // ==s8348:o36031:l20120:r18
            // ==s8348:o36033:l20120:r11
            // ==s8348:o36032:l20120:r2
            // // ==s4306:o36032:l20120:r13
            // BusControl bus = new BusControl();
            // VelocityEstimate ve = new VelocityEstimate(bus);
            // ve.Add(0, new Character [] {8400}, new Short [] {111});
            // ve.Add(26, new Character [] {8400}, new Short [] {111});
            // ve.Add(74, new Character [] {8348}, new Short [] {111});
            // ve.Add(96, new Character [] {8348}, new Short [] {111});
            // ve.Add(190, new Character [] {8348}, new Short [] {111});
            // ve.Add(231, new Character [] {8348}, new Short [] {111});
            // ve.Add(256, new Character [] {8348}, new Short [] {111});
            // ve.Add(274, new Character [] {8348}, new Short [] {111});
            // ve.Add(285, new Character [] {8348}, new Short [] {111});
            // ve.Add(287, new Character [] {4306}, new Short [] {111});
            //
            // System.out.println(ve.returnVelocity(0, 300));

            // ve.Add(0, new Character [] {10582, 6544}, new Short [] {11, 45,
            // -188});
            // ve.Add(20, new Character [] {6544, 8677}, new Short [] {11, 45,
            // -188});
            // ve.Add(50, new Character [] {8544, 8677, 8678}, new Short [] {11,
            // 45, -188});
            // ve.Add(75, new Character [] {8677, 8678}, new Short [] {11, 45,
            // -188});
            // System.out.println((char) 8677 + "  " + (char) 8678);
            // System.out.println("From 0 to 75: " + ve.returnVelocity(0, 75));
            // System.out.println("Squared at 50: " +
            // ve.returnVelocityLeastSquaredFitLine(50));
            // System.out.println("Naive at 50: " + ve.returnVelocityNaive(50));
            // System.out.println("Squared at 30: " +
            // ve.returnVelocityLeastSquaredFitLine(30));
            // System.out.println("Naive at 30: " + ve.returnVelocityNaive(30));

            // VelocityEstimateTest ve = new VelocityEstimateTest();
            // ve.Add(0, 10.760310896383146, 106.68376623946023);
            // ve.Add(20, 10.762288496600146, 106.68304947738163);
            // ve.Add(50, 10.766016386616375, 106.68280210189226);
            // ve.Add(75, 10.767588141159706, 106.68513636423106);
            // System.out.println("From 0 to 75: " + ve.returnVelocity(0, 75));
            // System.out.println("Squared at 50: " +
            // ve.returnVelocityLeastSquaredFitLine(50));
            // System.out.println("Naive at 50: " + ve.returnVelocityNaive(50));
            // System.out.println("Squared at 30: " +
            // ve.returnVelocityLeastSquaredFitLine(30));
            // System.out.println("Naive at 30: " + ve.returnVelocityNaive(30));
            //

            // ReadWriteBinaryWrapper mm = new ReadWriteBinaryWrapper();
            // mm.ReadFromBinary();
            // LinkedList <Sequence> output = mm.ReadFromBinary();
            // for (Sequence sequence : output) {
            // System.out.println(sequence.BusRouteID + " ");
            //
            // }

            // Get all ROUTE
            // BusInfoDownloader busInfo = new BusInfoDownloader();
            // busInfo.getAllRouteFromInternet();

            // Get from file;
            // BusControl bus = new BusControl();
            // System.out.println(bus.getBusStop((char)9773).getAddress());
            // System.out.println(bus.getBusStop((char)8440).getAddress());
            // BusRoute route = bus.getBusRoute((short)-1);
            // System.out.println(route.toString());
            // for (int i = 0; i < route.BusStopIDList.size(); i++) {
            // System.out.println(bus.getBusStop(route.BusStopIDList.get(i)).getAddress()
            // + " ");
            // }

            // HMMWrapper hmm = new HMMWrapper();
            // hmm.trainMainCell();
            // hmm.testMultipleRuns(true);

            // hmm.trainMainCell();
            // hmm.testWithPrinting(22);
            // hmm.trainAndTestMultiplecellOneRun(10);
            // hmm.trainAndTestMultiplecellOneRun(20);
            // hmm.trainAndTestMultiplecellOneRun(30);
            // hmm.trainAndTestMultiplecellOneRun(45);
            // hmm.trainAndTestMultiplecellOneRun(60);
            // hmm.trainAndTestMultiplecellOneRun(120);
            // hmm.trainAndTestMultiplecellOneRun(180);
            // hmm.trainAndTestMultiplecellOneRun(240);
            // hmm.trainAndTestMultiplecellOneRun(300);
            //

            // DataValidator dv = new DataValidator();
            // dv.TestProducetrainsequence();

            // dv.trainAndTestMultiplecellOneRun(5);

            // AllBusStop busstops = new
            // AllBusStop("D:\\1.DOCUMENT\\Dropbox\\Thesis HiT\\Data\\_BACKUP\\allBusStop.txt");
            // BusStop temp = busstops.getBusStop(9712);
            // System.out.println(temp.getLine());

            // System.out.println(CoordinateConversion.LatLngDistance(10.758076899999999,
            // 106.7008108, 10.768076899999999, 106.7008108));

            // * ----------------------- Code to read from txt and then write to
            // XML
            // FileConverter fc = new FileConverter();
            // fc.WriteToXML("C:\\Users\\Aya Hikaru\\Desktop");
            // System.out.println("Done");
            // */
            // BusControl bc = new BusControl();
            // bc.calculateDistanceOfBusStops();

            // System.out.println((short)('⧼'));
            // Sequence test = seq.refineSequence();
            // System.out.println(seq.toString());
            // System.out.println(test.toString());
            // System.out.println(sm.lcs("1122334", "1234"));
            // System.out.println(sm.lcs(seq, seq2).toString());

            // FileWrapper fw;
            // try {
            // fw = new
            // FileWrapper("D:\\1.DOCUMENT\\Dropbox\\Thesis HiT\\_DATANEW\\TESTING\\2013-01-16-17-44-34_log.txt");
            // fw.read();
            // System.out.println("===");
            // fw.read();
            // } catch (Exception e) {
            // // TODO Auto-generated catch block
            // e.printStackTrace();
            // }

            // char a = -32000;
            // System.out.println(a + " ");
            // 65535 = -1

            // char a = (char)-1;
            // if (a == (char)-1)
            // {
            // System.out.println("Wrong");
            // }

            // long test = 0x00010001;
            // long test1 = test <<16;
            // long test2 = test >>>15;
            // System.out.println("0xffff0001 << 4 " + test1);
            // System.out.println("0xffff0001 >> 4 " + test2);

            // Hashtable<Integer, Double> test = new Hashtable<>();
            // test.put(1, 1.0);
            // test.put(3, 2.0);
            // test.put(5,3.0);
            // test.put(300,300.0);
            // Iterator<Integer> i = test.keySet().iterator();
            // while (i.hasNext())
            // {
            // Integer tmp = (Integer) i.next();
            // System.out.println(test.get(tmp));
            // }

            //testBusMatching();
            //testVelocityEstimate();
            //testPosibleBusroute();
            //calculateSequenceLength();

//                        testImproveVeloEstiSeq();
//                        testCheckParaImproveVeloEstiSeq();
//			testVelocityEstimateSequence();
            //testLCS();
//                        BusControl bc = new BusControl();
//                        System.out.println(bc.returnMarkerString());
//                        bc.countBusStops();
//                        bc.countDistanceBasedonBusStops();
            //           busFullRoute bfr = new busFullRoute();
            //                       bfr.getAllRoute();
//                       System.out.println(bfr.getRouteDistance());
//			testImproveVeloEstiSeqAlongOneSeq();

            testVeloEstiCellID();

            long endTime = System.currentTimeMillis();
            long searchTime = endTime - startTime;
            System.out.println("JOB DONE in: " + searchTime + "ms");
            //printUsage();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static void testLCS() {
        Sequence train = new Sequence();
        //ob, state, lac, rep
        train.add(new SequenceElement((char) 1, (char) 100, (char) 1, (char) 10));
        train.add(new SequenceElement((char) 1, (char) 101, (char) 1, (char) 10));
        train.add(new SequenceElement((char) 2, (char) 102, (char) 1, (char) 10));
        train.add(new SequenceElement((char) 2, (char) 103, (char) 1, (char) 10));
        train.add(new SequenceElement((char) 3, (char) 103, (char) 1, (char) 11));
        train.add(new SequenceElement((char) 3, (char) 103, (char) 2, (char) 4));
        //train.add(new SequenceElement((char)3, (char)103, (char)2, (char)11));
        //train.add(new SequenceElement((char)3, (char)103, (char)2, (char)20));
        System.out.println("TRAIN DATA REFINED:" + train.refineSequence().toString());

        Sequence test = new Sequence();
        test.add(new SequenceElement((char) 1, (char) 0, (char) 1, (char) 10));
        test.add(new SequenceElement((char) 1, (char) 0, (char) 1, (char) 5));
        test.add(new SequenceElement((char) 2, (char) 0, (char) 1, (char) 10));
        test.add(new SequenceElement((char) 2, (char) 0, (char) 1, (char) 15));
        test.add(new SequenceElement((char) 3, (char) 0, (char) 1, (char) 10));
        test.add(new SequenceElement((char) 3, (char) 0, (char) 2, (char) 5));
        System.out.println("TEST DATA REFINED:" + test.refineSequence().toString());


        SequenceMatching sm = new SequenceMatching(SequenceMatching.MODE_READ_FROM_BINARYFILE);
        test = test.refineSequence();
        Sequence result = sm.lcs(train, test);
        System.out.println("RESULT" + result.toString());

    }

    private static void testImproveVeloEstiSeq() throws IOException {
        ImproveVeloEstiSeqWrapper ivesw = new ImproveVeloEstiSeqWrapper();
        Writer outSeq = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(CONSTANTS.RESULT_FOLDER
                + "improveVeloEstiSeq_seq.txt"), "UTF-8"));

        Writer outExcel = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(CONSTANTS.RESULT_FOLDER
                + "improveVeloEstiSeq_excel.txt"), "UTF-8"));

        ivesw.testSequenceAndPrintToFile(600, outSeq, outExcel);
    }

    private static void testImproveVeloEstiSeqAlongOneSeq() throws IOException {
        ImproveVeloEstiSeqWrapper ivesw = new ImproveVeloEstiSeqWrapper();
        Writer outSummary = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(CONSTANTS.RESULT_FOLDER
                + "improveVeloEstiSeqAlongOneSeq_summary.txt"), "UTF-8"));

        Writer outExcel = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(CONSTANTS.RESULT_FOLDER
                + "improveVeloEstiSeqAlongOneSeq_excel.txt"), "UTF-8"));

        ivesw.testOneSequenceAndPrintToFile(2, 20, outSummary, outExcel);
    }

    private static void testVelocityEstimateSequence() throws IOException {
        VelocityEstimateSequenceWrapper vesw = new VelocityEstimateSequenceWrapper();
        Writer outSummary = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(CONSTANTS.RESULT_FOLDER
                + "testSequenceAndPrintToFile_summary.txt"), "UTF-8"));

        Writer outExcel = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(CONSTANTS.RESULT_FOLDER
                + "testSequenceAndPrintToFile_summaryexcel.txt"), "UTF-8"));

        vesw.testSequenceAndPrintToFile(600, outSummary, outExcel);
    }

    private static void testPosibleBusroute() {
        SequenceMatching sm = new SequenceMatching(
                SequenceMatching.MODE_READ_FROM_TRAINFOLDER);
        LinkedList<Character> observationList = new LinkedList<Character>();
        observationList.add((char) 40913); // 14 , 10 , 151
        observationList.add((char) 43264); // 14
        LinkedList<Short> result = sm.possibleBusRoute(observationList);
        for (Short n : result) {
            System.out.println("Possible busRoute: " + (int) n);
        }
    }

    private static void calculateSequenceLength() {
        SequenceMatching sm = new SequenceMatching(
                SequenceMatching.MODE_READ_FROM_TRAINFOLDER);
        try {
            Writer out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(CONSTANTS.RESULT_FOLDER
                    + "sequencelength_summary.txt"), "UTF-8"));
            for (int i = 0; i < sm.seqs.size(); i++) {
                out.write(sm.seqs.get(i).BusRouteID + " "
                        + sm.seqs.get(i).size() + " "
                        + sm.refined_seqs.get(i).size() + "\n");
            }
            out.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private static void testBusMatching() {
        BusrouteMatchingWrapper bmw = new BusrouteMatchingWrapper();
        Writer out, outexcel;
        try {
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(CONSTANTS.RESULT_FOLDER
                    + "busstopmatching_summary.txt"), "UTF-8"));
            outexcel = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(CONSTANTS.RESULT_FOLDER
                    + "busstopmatching_summaryexcel.txt"), "UTF-8"));

            out.write(CONSTANTS.TRAIN_DATA_FOLDER + "\n");
            out.write(CONSTANTS.TEST_DATA_FOLDER + "\n\n\n");

            testBusMatchingWriteSummary(10, out, outexcel, bmw);
            testBusMatchingWriteSummary(20, out, outexcel, bmw);
            testBusMatchingWriteSummary(30, out, outexcel, bmw);
            testBusMatchingWriteSummary(45, out, outexcel, bmw);
            testBusMatchingWriteSummary(60, out, outexcel, bmw);
            testBusMatchingWriteSummary(90, out, outexcel, bmw);
            testBusMatchingWriteSummary(120, out, outexcel, bmw);
            testBusMatchingWriteSummary(180, out, outexcel, bmw);
            testBusMatchingWriteSummary(240, out, outexcel, bmw);
            testBusMatchingWriteSummary(300, out, outexcel, bmw);
            testBusMatchingWriteSummary(600, out, outexcel, bmw);

            out.close();
            outexcel.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static void testBusMatchingWriteSummary(int sequence_length,
            Writer outSummary, Writer outExcel, BusrouteMatchingWrapper bmw)
            throws IOException {
        long startTime, endTime, searchTime;
        startTime = System.currentTimeMillis();
        bmw.testBusStopAndPrintToFile(sequence_length, outSummary, outExcel);
        endTime = System.currentTimeMillis();
        searchTime = endTime - startTime;
        outExcel.write(" " + searchTime + "\n");
    }

    private static void testVelocityEstimate() {
        VelocityEstimateWrapper vew = new VelocityEstimateWrapper();
        Writer out, outexcel;
        try {
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(CONSTANTS.RESULT_FOLDER
                    + "busstopmatching_summary.txt"), "UTF-8"));
            outexcel = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(CONSTANTS.RESULT_FOLDER
                    + "busstopmatching_summaryexcel.txt"), "UTF-8"));

            out.write(CONSTANTS.TRAIN_DATA_FOLDER + "\n");
            out.write(CONSTANTS.TEST_DATA_FOLDER + "\n\n\n");

            // testVelocityEstimateWriteSummary(10, out, outexcel, vew);
            // testVelocityEstimateWriteSummary(20, out, outexcel, vew);
            // testVelocityEstimateWriteSummary(30, out, outexcel, vew);
            // testVelocityEstimateWriteSummary(45, out, outexcel, vew);
            // testVelocityEstimateWriteSummary(60, out, outexcel, vew);
            // testVelocityEstimateWriteSummary(90, out, outexcel, vew);
            // testVelocityEstimateWriteSummary(120, out, outexcel, vew);
            // testVelocityEstimateWriteSummary(180, out, outexcel, vew);
            // testVelocityEstimateWriteSummary(240, out, outexcel, vew);
            // testVelocityEstimateWriteSummary(300, out, outexcel, vew);
            // testVelocityEstimateWriteSummary(600, 60, out, outexcel, vew);
            // testVelocityEstimateWriteSummary(600, 120, out, outexcel, vew);
            // testVelocityEstimateWriteSummary(600, 180, out, outexcel, vew);
            testVelocityEstimateWriteSummary(600, 300, out, outexcel, vew);
            // testVelocityEstimateWriteSummary(600, 360, out, outexcel, vew);
            // testVelocityEstimateWriteSummary(600, 420, out, outexcel, vew);
            // testVelocityEstimateWriteSummary(600, 480, out, outexcel, vew);
            // testVelocityEstimateWriteSummary(600, 540, out, outexcel, vew);
            // testVelocityEstimateWriteSummary(600, 600, out, outexcel, vew);

            out.close();
            outexcel.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static void testVelocityEstimateWriteSummary(int sequence_length,
            int consider_length, Writer outSummary, Writer outExcel,
            VelocityEstimateWrapper vew) throws IOException {
        long startTime, endTime, searchTime;
        startTime = System.currentTimeMillis();
        vew.testBusStopAndPrintToFile(sequence_length, outSummary,
                consider_length, outExcel);
        endTime = System.currentTimeMillis();
        searchTime = endTime - startTime;
        outExcel.write(" " + searchTime + "\n");
    }

    private static void printUsage() {
        System.out.println("=====================");
        OperatingSystemMXBean operatingSystemMXBean = ManagementFactory
                .getOperatingSystemMXBean();
        for (Method method : operatingSystemMXBean.getClass()
                .getDeclaredMethods()) {
            method.setAccessible(true);
            if (method.getName().startsWith("get")
                    && Modifier.isPublic(method.getModifiers())) {
                Object value;
                try {
                    value = method.invoke(operatingSystemMXBean);
                } catch (Exception e) {
                    value = e;
                } // try
                System.out.println(method.getName() + " = " + value);
            } // if
        } // for
    }

    public static Sequence StringToSequence(String test) {
        String[] stringsplit = test.split("s|:|o|r|=");
        int control = 0;
        Character c = '0';
        int repeat = 0;
        Sequence t = new Sequence();
        char o = '0';
        for (String s : stringsplit) {
            if (s.compareTo("") == 0) {
                continue;
            }
            if (control == 0) {
                c = (char) Integer.parseInt(s);
                control++;
            } else if (control == 1) {
                o = (char) Integer.parseInt(s);
                control++;
            } else if (control == 2) {
                control++;
            } else if (control == 3) {
                repeat = Integer.parseInt(s);
                control = 0;
                t.add(new SequenceElement(o, c, (char) 0,
                        (char) repeat));
            }

        }
        return t;
    }

    private static void testCheckParaImproveVeloEstiSeq() throws IOException {
        ImproveVeloEstiSeqWrapper ivesw = new ImproveVeloEstiSeqWrapper();
//		Writer outSeq = new BufferedWriter(new OutputStreamWriter(
//				new FileOutputStream(CONSTANTS.RESULT_FOLDER
//						+ "improveVeloEstiSeqPara_seq.txt"), "UTF-8"));

        Writer outExcel = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(CONSTANTS.RESULT_FOLDER
                + "improveVeloEstiSeqPara_excel.txt"), "UTF-8"));

        ivesw.testSequenceCheckPara(600, outExcel);
    }

    private static void testVeloEstiCellID() {
        VeloEstiCellIDWrapper vecw = new VeloEstiCellIDWrapper();
        try {
        Writer outSeq = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(CONSTANTS.RESULT_FOLDER
                + "VeloEstiCellID_seq.txt"), "UTF-8"));

        Writer outExcel = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(CONSTANTS.RESULT_FOLDER
                + "VeloEstiCellID_excel.txt"), "UTF-8"));

        vecw.testSequenceAndPrintToFile(600, 60, outSeq, outExcel);
        } catch (IOException ex)
        {
            System.err.println(ex.getMessage());
        }

    }
}
